// 对Date的扩展，将 Date 转化为指定格式的String
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};
// 格式化时间
function timeFormatter(value) {
    return !value || value == '1000' ? "-" : new Date(value).Format("yyyy-MM-dd HH:mm:ss");
}

//判断是否为null "" undefined "  "
function isEmpty(str) {
    return str == null||str == undefined||str == ""||str.trim().length ==0;
}

//成功提示信息
var showMsgSuccess = function (msg) {
    $.confirm({
        type: 'green',
        title: false,
        content: msg ? msg : "保存成功",
        autoClose: 'confirm|3000',
        backgroundDismiss: true,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-success'
            }
        }
    });
};

//失败提示信息
var showMsgFail = function (errmsg) {
    $.confirm({
        type: 'red',
        animation: 'rotateX',
        closeAnimation: 'rotateX',
        title: false,
        content: errmsg ? errmsg : "保存失败",
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-danger'
            }
        }
    });
};

function confirm(msg, callback) {
    $.confirm({
        type: 'red',
        animationSpeed: 300,
        title: false,
        content: msg,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-danger',
                action: function () {
                    callback();
                }
            },
            cancel:{
                text: '取消',
                btnClass: 'btn-default'
            }
        }
    });
}

// 查询表单数据处理
$.fn.serializeJsonObject = function () {
    var json = {};
    var form = this.serializeArray();
    $.each(form, function () {
        if (json[this.name]) {
            if (!json[this.name].push) {
                json[this.name] = [json[this.name]];
            }
            json[this.name].push();
        } else {
            json[this.name] = this.value || '';
        }
    });
    return json;
};

//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}

//会员登陆
function login() {
    var name = $('input[name="name"]').val();
    var password = $('input[name="password"]').val();
    $.ajax({
        url: "/auth/login",
        type: "POST",
        data: "name=" + name + "&password=" + password,
        success: function(res) {
            if (res.code != "0200") {
                $('#right_msg').text(res.message ? res.message : "请求失败");
                return false;
            }
            $('#right_msg').text("");
            var refer = getUrlParam('refer');
            location.href = refer ? refer : "/site/";
        },
        error: function(e) {
            $('#right_msg').text("请求失败");
        }
    });
}

//会员退出
function logout() {
    $.ajax({
        url: "/auth/logout",
        type: "GET",
        success: function(res) {
            if (res.code != "0200") {
                showMsgFail(res.message ? res.message : "请求失败");
                return false;
            }
            location.href = "/auth/login";
        },
        error: function(e) {
            showMsgFail("请求失败");
        }
    });
}